package name.zxw.rabbitmq.priority;

import com.rabbitmq.client.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author zhangxinwang
 * @date 2022/6/22 16:44
 */
public class MyPublisher {

    public static void main(String[] args) throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        Channel channel = factory.newConnection().createChannel();

        channel.exchangeDeclare("priority", BuiltinExchangeType.DIRECT);


        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-max-priority", 10); //设置最大优先级
        arguments.put("x-queue-model","lazy"); //取值为"default"和"lazy",默认是"default",这里是设置为惰性队列的意思
        channel.queueDeclare("console", false, false, false, arguments);
        channel.queueBind("console", "priority", "info");

        Scanner scanner = new Scanner(System.in);
        System.out.println("等待发送消息...");
        int n = 0;
        while (scanner.hasNext()) {
            String message = scanner.next();
            AMQP.BasicProperties props = new AMQP.BasicProperties().builder().priority(n++).build();
            channel.basicPublish("priority", "info", props, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("发送信息:" + message);
        }


    }


}
